import time

import allure
from selenium.webdriver.common.by import By

from src.main.page.base_page import base_page
from .add_member_page import Add_member_page
from ..utils.Loguru import logger

class Contact_page(base_page):
    def __init__(self, driver):
        super().__init__(driver)

    @allure.step('前往添加成员页面')
    def goto_add_member_page(self):
        time.sleep(2)
        logger.info('前往添加成员页面')
        _add_member_locate=(By.XPATH,"//*[@class='ww_operationBar']//*[@class='qui_btn ww_btn js_add_member']")

        self.find_ele_is_presence(_add_member_locate).click()

        return Add_member_page(self.driver)

    @allure.step('获取所有的成员列表-phone')
    def get_member_list_phone(self):
        logger.info('获取成员列表')
        time.sleep(5)
        _get_member_list_locate_phone = (By.XPATH, "//*[@id='member_list']//tr/td[5]/span")

        # 返回成员列表
        span_eles = self.find_eles_is_presence(_get_member_list_locate_phone)


        return span_eles

    @allure.step('获取所有的成员列表-名称')
    def get_member_list(self):
        logger.info('获取成员列表')
        time.sleep(5)
        _get_member_list_locate_name=(By.XPATH,"//*[@id='member_list']//tr/td[2]/span")
        #返回成员列表
        span_eles = self.find_eles_is_presence(_get_member_list_locate_name)

        return span_eles

    @allure.step('删除单个成员')
    def delete_member_single(self,index):
        #通过勾选指定位置的成员选择框，来进行删除
        logger.info('删除单个成员')
        _input_list_locate = (By.XPATH,"//*[@id='member_list']//tr/td[1]/input")

        input_list = self.find_eles_is_presence(_input_list_locate)

        input_list[index].click()

        _delete_btn_locate =(By.XPATH,"//*[@class='qui_btn ww_btn js_delete']")
        self.find_ele_is_presence(_delete_btn_locate).click()
        _confirm_btn_locate=(By.XPATH,"//*[@class='qui_btn ww_btn ww_btn_Blue']")
        self.find_ele_is_presence(_confirm_btn_locate).click()

    @allure.step('批量删除成员')
    def delete_member(self,index_list):
        #通过勾选指定位置的成员选择框，来进行删除
        logger.info('批量删除成员')
        _input_list_locate = (By.XPATH,"//*[@id='member_list']//tr/td[1]/input")

        input_list = self.find_eles_is_presence(_input_list_locate)
        for index in index_list:
            input_list[index].click()

        _delete_btn_locate =(By.XPATH,"//*[@class='qui_btn ww_btn js_delete']")
        self.find_ele_is_presence(_delete_btn_locate).click()
        _confirm_btn_locate=(By.XPATH,"//*[@class='qui_btn ww_btn ww_btn_Blue']")
        self.find_ele_is_presence(_confirm_btn_locate).click()

    @allure.step('添加子部门')
    #点击部门栏的三个点，添加子部门
    def click_deparkment_btn_add_department(self):
        logger.info('添加子部门')
        department_name = '冼喆之家-' + self.get_faker_name()
        _deparkment_btn_locate=(By.XPATH,"//*[@class='jstree-anchor jstree-clicked']/span")
        self.find_ele_is_presence(_deparkment_btn_locate).click()

        _add_department_btn_locate=(By.XPATH,"//*[@class='vakata-context jstree-contextmenu jstree-default-contextmenu']/li[1]")
        self.find_ele_is_presence(_add_department_btn_locate).click()

        _input_department_name_locate=(By.XPATH,"//*[@class='qui_inputText ww_inputText']")
        self.find_ele_is_presence(_input_department_name_locate).send_keys(department_name)

        _save_btn_locate=(By.XPATH,"//*[@class='qui_btn ww_btn ww_btn_Blue']")
        self.find_ele_is_presence(_save_btn_locate).click()
        return department_name

    @allure.step('往新添加的子部门里添加成员')
    #添加完部门后，往该部门里添加成员
    def add_member_after_add_department(self):
        logger.info('往新添加的子部门里添加成员')
        time.sleep(5)
        _add_member_btn_locate=(By.XPATH,"//*[@class='qui_btn ww_btn js_add_member']")
        # self.find_ele_is_clickable(_add_member_btn_locate).click()
        self.find_ele_is_presence(_add_member_btn_locate).click()
        return Add_member_page(self.driver)

    @allure.step('添加成员后删除成员')
    #添加完成员后，删除成员
    def delete_member_in_somedepartment(self):
        logger.info('添加成员后删除成员')
        _click_member_btn_locate=(By.XPATH,"//*[@class='js_checkbox_container member_colRight_memberTable_td member_colRight_memberTable_td_Checkbox']/input")
        self.find_ele_is_presence(_click_member_btn_locate).click()

        _delete_btn_locate=(By.XPATH,"//*[@class='qui_btn ww_btn js_delete']")
        self.find_ele_is_presence(_delete_btn_locate).click()
        _confirm_delete_btn_locate = (By.XPATH,"//*[@class='qui_btn ww_btn ww_btn_Blue']")
        self.find_ele_is_presence(_confirm_delete_btn_locate).click()

    @allure.step('关闭子部门')
    #删除完成员后，删除刚生成的子部门
    def delete_department(self):
        logger.info('关闭子部门')
        _department_more_choie_btn_locate=(By.XPATH,"//*[@class='jstree-anchor jstree-clicked']/span")
        self.find_ele_is_presence(_department_more_choie_btn_locate).click()
        _delete_btn_locate=(By.XPATH,"//*[@class='vakata-context jstree-contextmenu jstree-default-contextmenu']//*[text()='删除']")
        self.find_ele_is_presence(_delete_btn_locate).click()
        _confirm_delete_btn_locate=(By.XPATH,"//*[@class='qui_btn ww_btn ww_btn_Blue']")
        self.find_ele_is_presence(_confirm_delete_btn_locate).click()


    #根据输入部门名字来添加成员
    @allure.step('根据输入部门名字来添加成员')
    def point_to_department_by_department_name(self,department_name):
        logger.info(f'选中指定的部门:{department_name}')
        _department_add_btn_locate=(By.XPATH,f"//*[@class='jstree-children']//a[text()=\'{department_name}\']")
        self.find_ele_is_presence(_department_add_btn_locate).click()

    @allure.step('选择指定部门')
    #根据输入的位置index来指定部门，并且往该部门添加成员
    def point_to_department_by_index(self,index):
        logger.info(f'选择第{index}个部门')
        #在xpath里index选列表是从1开始的
        _department_add_btn_locate=(By.XPATH,f"//*[@class='jstree-children']/li[{index}]/a")
        self.find_ele_is_presence(_department_add_btn_locate).click()

    @allure.step('文件导入添加成员')
    #一个已经存在成员的部门，点击文件导入
    def add_a_lot_member_when_exist(self):
        _add_a_lot_btn_locate=(By.XPATH,"//*[@class='ww_btnWithMenu ww_btnWithMenu_Open_Up js_btnWithMenu js_import_other_wrap']/a")
        self.find_ele_is_presence(_add_a_lot_btn_locate).click()

        _file_input_btn = (By.XPATH,"//*[@class='ww_btnWithMenu ww_btnWithMenu_Open_Up js_btnWithMenu js_import_other_wrap ww_btnWithMenu_Open']//a[@class='qui_dropdownMenu_itemLink ww_dropdownMenu_itemLink js_import_member']")

        self.find_ele_is_presence(_file_input_btn).click()
        _input_file_input=(By.XPATH,"//*[@class='qui_btn ww_btn ww_fileInputWrap']")
        logger.warning(self.send_input_file_path())

        self.find_ele_is_presence(_input_file_input).send_keys(self.send_input_file_path())

        _save_btn_locate=(By.XPATH,"//*[@id='submit_csv']")
        time.sleep(2)
        self.find_ele_is_presence(_save_btn_locate).click()
        _goto_see_locate = (By.XPATH,"//*[@class='qui_btn ww_btn ww_btn_Large ww_btn_Blue import_succReturnBtn']")
        self.find_ele_is_presence(_goto_see_locate).click()

    @allure.step('添加标签')
    #点击添加标签
    def tag(self):
        _btn_option_locate = (By.XPATH,"//*[@class='member_colLeft_top_addBtnWrap js_create_dropdown']")
        self.find_ele_is_presence(_btn_option_locate).click()
        _btn_add_tag_locate = (By.XPATH,"//*[@class='js_create_tag']")
        self.find_ele_is_presence(_btn_add_tag_locate).click()

        _input_tag_name_locate = (By.XPATH,"//*[@class='qui_inputText ww_inputText']")
        self.find_ele_is_presence(_input_tag_name_locate).send_keys('123')

        _drop_list_use_power_locate = (By.XPATH,"//*[@class='qui_btn ww_btn ww_btn_Dropdown js_toggle_share_range']")
        self.find_ele_is_presence(_drop_list_use_power_locate).click()
        #选择用户权限：位置0：所有管理员  位置1：指定人  位置2：仅自己
        _choice_user_locate = (By.XPATH,"//*[@class='qui_dropdownMenu ww_dropdownMenu js_share_range_container']//a")
        self.find_eles_is_presence(_choice_user_locate)[0].click()

        _drop_write_right_locate = (By.XPATH,"//*[@class='ww_btnWithMenu js_btnWithMenu js_share_dialog_perm_wrap js_share_dialog_perm_elem']/a")
        self.find_ele_is_presence(_drop_write_right_locate).click()

        _choice_write_right_locate=(By.XPATH,"//*[@class='qui_dropdownMenu ww_dropdownMenu member_tag_dialog_perm_select']//a")
        #选择编写权限，位置0为仅使用，位置1为可编写
        self.find_eles_is_presence(_choice_write_right_locate)[0].click()

    @allure.step('标签页面：添加成员')
    #点击进入标签页面后添加成员,选择任意部门或员工进行添加
    def add_member(self):
        #位置0为添加成员/部门，位置1为批量导入
        _btn_add_member_locate = (By.XPATH,"//*[@class='member_colRight_cnt_operation']/a")
        self.find_eles_is_presence(_btn_add_member_locate)[0].click()
        #选择任何一个员工或不萌
        _get_li_list_locate = (By.XPATH,"//*[@class='jstree-node jstree-last jstree-open']//*[@class='jstree-children']/li")
        self.find_eles_is_presence(_get_li_list_locate)[1].click()







